﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyDeTaiKHCN.Obj_class;
using System.IO;
namespace QuanLyDeTaiKHCN.UserControls.Others
{
    public partial class BanGiaoSanPhamDeTai : System.Web.UI.UserControl
    {
        public SecurityLib.Sec_AES _se = new SecurityLib.Sec_AES();
        _dbDataContext _db = new _dbDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _se.Decrypt(str, "gold", 128);
                if (_db.tblHopHoiDongNghiemThus.Where(r => r.MaDeTai.Trim() == madetai).Count() == 0)
                {
                    pndanhsachdetaiduan.Visible = false;
                    lblThongBao.Visible = true;
                    lblThongBao.Text = "Đề tài này chưa được họp hội đồng nghiệm thu";
                }
                else
                {
                    BindGrid(madetai);
                    BindGriddsDonVi();
                    pndanhsachdetaiduan.Visible = true;
                    lblThongBao.Visible = false;
                    lblThongBao.Text = "";
                }
            }
        }
        protected void BindGriddsDonVi()
        {
             string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _se.Decrypt(str, "gold", 128);
            var list = from p in _db.tblBanGiaoSanPhams.Where(r => r.MaDeTai.Trim() == madetai)
                       select new
                       {
                           ID=p.ID,
                           TenDonViNhan=p.TenDonViNhan.Trim(),
                           DaiDienDonVi=p.DaiDienDonVi.Trim(),
                           NgayNhan=string.Format("{0:dd/MM/yyyy}",p.NgayGiao),
                           NgayDangKyKetQuan=string.Format("{0:dd/MM/yyyy}",p.NgayDangKyKetQuan)
                       };
            griddsdonvinhanbangiao.DataSource = list.ToList();
            griddsdonvinhanbangiao.DataBind();
        }
        protected void LinkAddFile_Click(object sender, EventArgs e)
        {
            if (filebienban.HasFile)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _se.Decrypt(str, "gold", 128);
                DateTime t = DateTime.Now;
                string filename = (t.Day < 10 ? "0" + t.Day.ToString() : t.Day.ToString()) + (t.Month < 10 ? "0" + t.Month.ToString() : t.Month.ToString()) + t.Year.ToString() + t.Hour.ToString() + t.Minute.ToString() + t.Second.ToString() + Path.GetExtension(filebienban.FileName);
                tblFile _tb = new tblFile();
                _tb.MaDeTaiDuAn = madetai;
                _tb.FileThuyetMinh = filename;
                _tb.GhiChuFileThuyetMinh = txtghichufilethuyetminh.Text;
                _tb.NgayNhanThuyetMinh = CatalogAccess.ConvertToDate(txtNgayNhan.Text);
                _tb.FileCuaBuoc = 14;
                try
                {
                    _db.tblFiles.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    BindGrid(madetai);
                    filebienban.SaveAs(Server.MapPath(PageURL.local + "filehoso/" + filename));
                    txtghichufilethuyetminh.Text = "";
                }
                catch { }
            }
        }

        protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _se.Decrypt(str, "gold", 128);
            var item = _db.tblFiles.Single(r => r.ID == int.Parse(grid.DataKeys[e.RowIndex].Value.ToString()));
            try
            {
                _db.tblFiles.DeleteOnSubmit(item);
                _db.SubmitChanges();
                try
                {
                    string filename = ((Label)grid.Rows[e.RowIndex].FindControl("lblFileName")).Text;
                    System.IO.File.Delete(Server.MapPath(PageURL.local + "filehoso/" + filename));
                }
                catch { }
                BindGrid(madetai);
            }
            catch { }
        }
        protected void BindGrid(string madetai)
        {
            var list = from p in _db.tblFiles.Where(r => r.MaDeTaiDuAn.Trim() == madetai && r.FileCuaBuoc == 14)
                       select new
                       {
                           ID = p.ID,
                           TrichYeu = p.GhiChuFileThuyetMinh.Trim(),
                           NgayLuu = string.Format("{0:dd/MM/yyyy}", p.NgayNhanThuyetMinh),
                           FileThuyetMinh = p.FileThuyetMinh.Trim(),
                           //sau khi up len host thi xai link1:
                           //Fileonline =domain_name + "\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //test xaif link2
                           //Fileonline = "localhost\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //demo link3:
                           Fileonline = "http://thongkekhcnbd.khcnbinhduong.gov.vn/userfiles/vanban/9475707965_493_296.pdf"

                       };
            grid.DataSource = list.ToList();
            grid.DataBind();
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
             string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _se.Decrypt(str, "gold", 128);
            if (inputID.Value == "")
            {
                tblBanGiaoSanPham _tb = new tblBanGiaoSanPham();
                _tb.MaDeTai = madetai;
                _tb.NgayGiao = CatalogAccess.ConvertToDate(txtNgayBanGiaoSanPham.Text);
                if (CatalogAccess.ConvertToDateBool(txtNgayDangKyNhanKetQua.Text))
                    _tb.NgayDangKyKetQuan = CatalogAccess.ConvertToDate(txtNgayDangKyNhanKetQua.Text);
                _tb.TenDonViNhan = txtDonViNhanBanGiao.Text;
                _tb.DaiDienDonVi = txtDaiDienDonVi.Text;
                try
                {
                    _db.tblBanGiaoSanPhams.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu.";
                    BindGriddsDonVi();
                }
                catch { lblstatus.Text = "Không lưu được."; }
            }
            else
            {
                var _tb = _db.tblBanGiaoSanPhams.Single(r => r.ID == int.Parse(inputID.Value));
                _tb.NgayGiao = CatalogAccess.ConvertToDate(txtNgayBanGiaoSanPham.Text);
                if (CatalogAccess.ConvertToDateBool(txtNgayDangKyNhanKetQua.Text))
                    _tb.NgayDangKyKetQuan = CatalogAccess.ConvertToDate(txtNgayDangKyNhanKetQua.Text);
                _tb.TenDonViNhan = txtDonViNhanBanGiao.Text;
                _tb.DaiDienDonVi = txtDaiDienDonVi.Text;
                try
                {
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu.";
                    BindGriddsDonVi();
                }
                catch { lblstatus.Text = "Không lưu được."; }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            txtNgayBanGiaoSanPham.Text = "";
            txtDonViNhanBanGiao.Text = "";
            txtDaiDienDonVi.Text = "";
            inputID.Value = "";
        }

        protected void griddsdonvinhanbangiao_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var item = _db.tblBanGiaoSanPhams.Single(r => r.ID == int.Parse(griddsdonvinhanbangiao.DataKeys[e.RowIndex].Value.ToString()));
            try
            {
                _db.tblBanGiaoSanPhams.DeleteOnSubmit(item);
                _db.SubmitChanges();
                BindGriddsDonVi();
            }
            catch { }
        }

        protected void griddsdonvinhanbangiao_RowEditing(object sender, GridViewEditEventArgs e)
        {
            inputID.Value = ((Label)griddsdonvinhanbangiao.Rows[e.NewEditIndex].FindControl("lblID")).Text;
            txtNgayBanGiaoSanPham.Text = ((Label)griddsdonvinhanbangiao.Rows[e.NewEditIndex].FindControl("lblNgayNhan")).Text;
            txtDonViNhanBanGiao.Text = ((Label)griddsdonvinhanbangiao.Rows[e.NewEditIndex].FindControl("lblTenDonVi")).Text;
            txtDaiDienDonVi.Text = ((Label)griddsdonvinhanbangiao.Rows[e.NewEditIndex].FindControl("lblDaiDienDonVi")).Text;
            txtNgayDangKyNhanKetQua.Text = ((Label)griddsdonvinhanbangiao.Rows[e.NewEditIndex].FindControl("lblNgayDangKyNhanKetQua")).Text;

        }

    }
}